// mircoApp/store-oil-station/refueling_payment/refueling_payment.js
const app = getApp();
var location = require("../../../mybehaviors/location/location");

Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    storeId: "",
    stroeList: [],
    point: null,
    city: null,
    storeNameKey: "",
    full_address: "",
    page: 1,
    total_page: 0,
    orderList: [],
    codeIndex: null,
    gunIndex: null,
    show: false,
    showGun: false,
    oil_list: [],
    gun_list: [],
    gun_list_select: "",
    oil_list_select: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init();
  },
  init() {
    this.setData({
      codeIndex: null,
      gunIndex: null,
      show: false,
      showGun: false
    })
  },
  showPopup(e) {
    let {
      key
    } = e.currentTarget.dataset;
    wx.showModal({
      title: '',
      content: key,
      showCancel: false
    })
  },
  // tapSelectCodeIndex(e) {
  //   let {
  //     index,
  //     item
  //   } = e.currentTarget.dataset;
  //   if (this.data.oil_list_select && this.data.oil_list_select.code == item.code) {
  //     this.setData({
  //       oil_list_select: {
  //         code: '',
  //         name: ''
  //       },
  //       codeIndex: null
  //     })
  //     return
  //   }
  //   this.setData({
  //     codeIndex: index,
  //     oil_list_select: item
  //   })
  // },
  // tapSelectgunIndex(e) {
  //   let {
  //     index,
  //     item
  //   } = e.currentTarget.dataset;
  //   if (this.data.gun_list_select == item) {
  //     this.setData({
  //       gun_list_select: "",
  //       gunIndex: ""
  //     })
  //     return;
  //   }
  //   this.setData({
  //     gun_list_select: item,
  //     gunIndex: index
  //   })
  // },
  toStoreList() {
    wx.navigateTo({
      url: '/mircoApp/store-oil-station/refueling_list/refueling_list?store_id=' + this.data.storeId,
    })
  },
  goToAdress() {
    let latitude = this.data.storeList.latitude;
    let longitude = this.data.storeList.longitude;
    let name = this.data.storeList.store_name;
    let point = app.BMapTransqqMap(parseFloat(longitude), parseFloat(latitude));
    wx.openLocation({
      latitude: point.lat,
      longitude: point.lng,
      scale: 18,
      name
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  tapStoreID(e) {
    let {
      item
    } = e.currentTarget.dataset;
    this.setData({
      storeId: item.id,
      full_address: item.full_address
    })
    this.showClose();
    this.getStore();
  },
  showClose() {
    this.setData({
      page: 1,
      total_page: 0,
      stroeList: [],
      show: false
    })
  },
  toPayM(e) {
    let {
      item
    } = e.currentTarget.dataset;
    console.log(item)
    wx.navigateTo({
      url: `/packageD/buy/myOrder_v2/myOrder_v2?goodsId=${item.goodsId}&optionsId=&total=1&tag=oil_order&plate_num=${item.plate_num}&litre=${item.litre}&sn=${item.third_order_no}&oil_gun=${item.oil_gun}&source=${item.order_source}&amt=${item.amount}`,
    })
  },
  async getData() {
    let json = {};
    console.log('ddddddds')
    await this._getLocation((res) => {
      console.log('ddddddds')
      json.city = res.address_component.city;
      json.lng = res.location.lng;
      json.lat = res.location.lat;
      this.data.point = {
        lng: res.location.lng,
        lat: res.location.lat,
        city: res.address_component.city
      }
      this.setData({
        full_address: res.address,
        page: 1,
        total_page: 1
      })
      this.firstGetStore();
    }, (e) => {
      console.log(e)
      wx.showModal({
        title: '定位失败',
        content: '请确认授权定位是否开启',
        confirmText: '手动定位',
        complete: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/packageC/o2o/o2oLocation_loc/o2oLocation_loc?tag=oil',
            })
          }
        }
      })
    })
  },
  async firstGetStore() {
    let json = {
      lng: this.data.point.lng,
      lat: this.data.point.lat,
      city: this.data.city,
      name: this.data.storeNameKey
    };
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.store-oil-station.frontend.unpaid-order.store"),
      data: json
    })
    if (data.result) {
      let store = data.data.data;
      if (this.options.store_id) {
        store.forEach(element => {
          if (element.id == this.options.store_id) {
            this.data.storeId = element.id;
            this.data.storeList = element;
          }
        });
        this.setData({
          storeId: this.data.storeId,
          storeList: this.data.storeList
        })
      } else {
        if (store.length > 0) {
          store.forEach(element => {
            if (element.is_default) {
              this.data.storeId = element.id;
              this.data.storeList = element;
            }
          });
          if (!this.storeId) {
            this.data.storeId = store[0].id;
          }
          this.data.storeList = store[0];
          this.setData({
            storeId: this.data.storeId,
            storeList: this.data.storeList
          })
        }
      }
      this.getStore();
    } else {
      app.tips(data.msg)
    }
  },
  tapSelectCodeIndex_radio(e) {
    let {
      item
    } = e.currentTarget.dataset;
    this.setData({
      orderList: []
    })
    if (item.code == this.data.oil_list_select) {
      this.setData({
        oil_list_select: ""
      })
      this.confirmTap();
      return;
    }
    this.setData({
      oil_list_select: item.code
    })
    this.confirmTap();
  },
  tapSelectgunIndex(e) {
    let {
      item
    } = e.currentTarget.dataset;
    this.setData({
      orderList: []
    })
    if (item == this.data.gun_list_select) {
      this.setData({
        gun_list_select: ""
      })
      this.confirmTap();
      return;
    }
    this.setData({
      gun_list_select: item
    })
    this.confirmTap();
  },
  showGunOpen() {
    this.setData({
      showGun: true
    })
  },
  showOpen() {
    this.setData({
      show: true
    })
  },
  async getStore() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.store-oil-station.frontend.unpaid-order.wait-pay"),
      data: {
        sid: this.data.storeId
      }
    })
    if (data.result) {
      if (data.data) {
        this.setData({
          orderList: data.data?.order_list || [],
          gun_list: data.data.gun_list,
          oil_list: data.data.oil_list
        })
      }
    } else {
      app.tips(data.msg);
      this.setData({
        orderList: [],
        gun_list: [],
        oil_list: []
      })
    }
  },
  showClose() {
    this.setData({
      show: false,
    })
  },
  showGunClose() {
    this.setData({
      showGun: false
    })
  },
  async confirmTap() {
    let json = {
      sid: this.data.storeId,
      oil_code: this.data.oil_list_select,
      gun: this.data.gun_list_select
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.store-oil-station.frontend.unpaid-order.search-wait-pay"),
      data: json
    })
    if (data.result) {
      this.setData({
        showGun: false,
        show: false,
        orderList: data.data
      })
    } else {
      app.tips(data.msg);
      this.setData({
        showGun: false,
        show: false
      })
    }
  },
  // async getStoreList(e, key = false) {
  //   let json = {
  //     lng: this.data.point?.lng,
  //     lat: this.data.point?.lat,
  //     city: this.data?.city,
  //     name: this.data.storeNameKey
  //   }
  //   if (key) {
  //     this.setData({
  //       page: this.data.page + 1
  //     })
  //     json.page = this.data.page;
  //   }
  //   let {
  //     data
  //   } = await app._postNetWork({
  //     url: app.getNetAddresss("plugin.store-oil-station.frontend.oil-store.index"),
  //     data: json
  //   })
  //   if (data.result) {
  //     if (key) {
  //       this.setData({
  //         stroeList: this.data.stroeList.concat(data.data.data)
  //       })
  //       return
  //     }
  //     this.setData({
  //       page: 1,
  //       total_page: data.data.last_page,
  //       stroeList: data.data.data,
  //       show: true,
  //     })
  //   } else {
  //     if (key) {
  //       this.setData({
  //         page: this.data.page - 1,
  //         total_page: 0
  //       })
  //       return
  //     }
  //     app.tips(data.msg);
  //   }
  // }
})